package com.wh.controller;

import com.wh.common.ApiRestResponse;
import com.wh.exception.SytException;
import com.wh.exception.SytExceptionEnum;
import com.wh.model.vo.UserLoginVo;
import com.wh.model.vo.UserRegisterVo;
import com.wh.service.UserService;
import com.wh.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.regex.Pattern;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/11/15 21:12
 * @description:
 * @version:
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;
    //用户注册
    @PostMapping("/register")
    public ApiRestResponse register(@Valid @RequestBody UserRegisterVo userRegisterVo){
        String mobile = userRegisterVo.getMobile();
        Pattern pattern = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9]))\\d{8}$+");
        if(!pattern.matcher(mobile).find()){
            return ApiRestResponse.error(SytExceptionEnum.MOBILE_WRONG);
        }
        userService.register(userRegisterVo);
        return ApiRestResponse.success("注册成功");
    }


    //用户密码登录
    @PostMapping("/login/password")
    public ApiRestResponse login(@RequestBody UserLoginVo userLoginVo){
        return userService.login(userLoginVo);
    }



    //用户密码登录
    @PostMapping("/test")
    public ApiRestResponse test(HttpServletRequest request){
        return ApiRestResponse.success();
    }




}
